package design.principle;

import java.text.NumberFormat;
import java.util.ArrayList;

/**
 * @program JavaBase
 * @description: 开闭原则
 * @author: zhanglu
 * @create: 2020-03-26 18:16:00
 */
public class OpenShut {


    public static void main(String[] args) {

        BookStore.show();


        //开闭原则，拓展子类的方式；应对调用方也要有些修改
        BookStore.show2();

    }


}

interface Ibook{

    String getName();

    int getPrice();

    String getAuthor();

}

class NoveIbook implements Ibook{

    private String name;
    private int price;
    private String author;

    public NoveIbook(String name, int price, String author){
        this.name = name;
        this.price = price;
        this.author = author;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public int getPrice() {
        return this.price;
    }

    @Override
    public String getAuthor() {
        return this.author;
    }

}

class BookStore{

    private final static ArrayList<Ibook> bookList = new ArrayList<Ibook>();
    private final static ArrayList<Ibook> offNoveIbookList = new ArrayList<Ibook>();
    //static静态模块初始化数据，实际项目中一般是由持久层完成
    static{
        bookList.add(new NoveIbook("天龙八部",3200,"金庸"));
        bookList.add(new NoveIbook("巴黎圣母院",5600,"雨果"));
        bookList.add(new NoveIbook("悲惨世界",3500,"雨果"));
        bookList.add(new NoveIbook("金瓶梅",4300,"兰陵笑笑生"));


        offNoveIbookList.add(new OffNoveIbook("天龙八部",3200,"金庸"));
        offNoveIbookList.add(new OffNoveIbook("巴黎圣母院",5600,"雨果"));
        offNoveIbookList.add(new OffNoveIbook("悲惨世界",3500,"雨果"));
        offNoveIbookList.add(new OffNoveIbook("金瓶梅",4300,"兰陵笑笑生"));
    }

    //模拟书店买书
    public static void show(){
        NumberFormat formatter = NumberFormat.getCurrencyInstance();
        formatter.setMaximumFractionDigits(2);
        System.out.println("-----------书店卖出去的书籍记录如下：-----------");
        for(Ibook book:bookList){
            System.out.println("书籍名称：" + book.getName()+"\t书籍作者：" + book.getAuthor()+"\t书籍价格："+ formatter.format (book.getPrice()/ 100.0)+"元");
        }
    }

    //模拟书店买书
    public static void show2(){
        NumberFormat formatter = NumberFormat.getCurrencyInstance();
        formatter.setMaximumFractionDigits(2);
        System.out.println("-----------书店卖出去的书籍记录如下：-----------");
        for(Ibook book:offNoveIbookList){
            System.out.println("书籍名称：" + book.getName()+"\t书籍作者：" + book.getAuthor()+"\t书籍价格："+ formatter.format (book.getPrice()/ 100.0)+"元");
        }
    }

}

class OffNoveIbook extends NoveIbook{

    public OffNoveIbook(String name, int price, String author) {
        super(name, price, author);
    }

    @Override
    public int getPrice() {
        //原价
        int selfPrice = super.getPrice();

        int offPrice=0;
        if(selfPrice>4000){ //原价大于40元，则打9折
            offPrice = selfPrice * 90 /100;
        }else{
            offPrice = selfPrice * 80 /100;
        }
        return offPrice;
    }
}

